home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18462 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  87 lines

  1. Path: ix.netcom.com!news
  2. From: Bradd W. Szonye <bradds@ix.netcom.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: RE: int::~int()
  5. Date: 20 Apr 1996 16:45:21 GMT
  6. Organization: Netcom
  7. Message-ID: <01bb2ed9.363f0020$65c2b7c7@Zany.localhost>
  8. References: <199604160100.KAA06143@public1.guangzhou.gd.cn>
  9. NNTP-Posting-Host: det-mi3-05.ix.netcom.com
  10. X-NETCOM-Date: Sat Apr 20 11:45:21 AM CDT 1996
  11. X-Newsreader: Microsoft Internet News
  12.  
  13.  
  14. On Monday, April 15, 1996, Wang TianXing wrote...
  15. > On Sun, 14 Apr 1996 13:49:59 +0900, Xu Yifeng <jafd@public.sta.net.cn>
  16. > wrote:
  17. > | Hi everybody,
  18. > | can your compiler compile following program?
  19. > | //------------------------
  20. > | void main()
  21. > | {
  22. > |      int i = 0;
  23. > |      i.int::~int();
  24. > | }
  25. > | //------------------------
  26. > | is it a legal C++ program?
  27. > Though many guys had said it is a legal one, I don't think it's a
  28. > meaningful one.  The Sep, 95 draft Standard says in 12.4:
  29. > 12 Invocation  of  destructors  is  subject to the usual rules for
  30. >    member functions (_class.mfct_), e.g., an object of the
  31. >    appropriate  type  is required  (except  invoking  delete  on a
  32. >    null pointer has no effect). Once a destructor is invoked for an
  33. >    object,  the  object  no  longer exists;  the behavior is
  34. >    undefined if the destructor is invoked for an object whose
  35. >    lifetime has  ended  (_basic.life_).   [Example:  if  the
  36. >    destructor  for  an  automatic  object  is explicitly invoked,
  37. >    and the block is subsequently left in a manner that  would
  38. >    ordinarily invoke implicit destruction of the object, the
  39. >    behavior is undefined.  ]
  40. > The [Example:...], though it isn't part of the standard, says
  41. > something directly related to your program.
  42. > Although there're some differences between "well-formed program" and
  43. > "undefined behavior", I don't think it's a bad idea that a compiler
  44. > rejects this sort of code, as it will surely cause undefined
  45. > behaviors.
  46. > The plain old Borland C++ 3.1 compiles
  47. >     int i = 0;
  48. >     (&i)->int::~int();
  49. > just fine.  And that's the "Standard" way(via pointer) to explicitly
  50. > call a destructor.
  51.  
  52. The example is contrived; you wouldn't ever want to call a destructor like
  53. that for an automatic object. You might want to do something like that,
  54. though, if you were dealing with a reference to a heap object in a
  55. template class. For another contrived example:
  56.  
  57. template <class T> class Foo {
  58. public:
  59.     Foo(T* bar) { Destroy(*bar); }
  60.     void Destroy(T& bar) { bar.T::~T(); }
  61. }
  62.  
  63. void Bar()
  64. {
  65.     char* s = new char[sizeof(int)];
  66.     Foo<int> test((int*)s);
  67. }
  68.  
  69. That's the kind of circumstance that happens in the STL; you want the
  70. "destroy" function to invoke a destructor on raw memory, regardless of
  71. what type the destroyed object is.
  72.  
  73.  
  74.  
  75.